package com.hxzy.commons.helper;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <p>
 * 类描述：菜单加载工具类
 * <p>
 * 创建人：Abdi
 * <p>
 * 创建时间：2013-10-20 下午03:21:11
 */
public class MenuHelper {
	/**
	 * 菜单Map
	 */
	private static Map<String,Map<String,List<Map<String,String>>>> menuMap = new HashMap<String, Map<String,List<Map<String,String>>>>();
	
	/**
	 * 菜单路径
	 */
	private static String menuXmlPath;
	
	/**
	 * 最后修改时间
	 */
	private static long modefiedTime;
	
	private static Map<String,String> nameMap = new HashMap<String, String>();

	static{
		SAXReader reader = new SAXReader();
		try {
			menuXmlPath = MenuHelper.class.getClassLoader().getResource("menu.xml").getPath();
			Document doc = reader.read(menuXmlPath);
			Element root = doc.getRootElement();
			loadMainMenu(root);
			
			//保存最后修改时间
			File file = new File(menuXmlPath);
			modefiedTime = file.lastModified();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("===>>> 文件["+menuXmlPath+"]不存在!");
		}
	}
	
	/**
	 * 根据ID与角色加载菜单
	 * @param menuid
	 * @param role
	 * @return
	 */
	public static List<Map<String,String>> loadMenu(String menuid,String role){
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		try {
			File file = new File(menuXmlPath);
			long lastModifiedTime = file.lastModified();
			if(lastModifiedTime != modefiedTime){//监听文件是否发生变化
				menuMap.clear();
				nameMap.clear();
				reloadMenu();
			}
			list = menuMap.get(menuid).get("public");
		} catch (Exception e) {
			throw new RuntimeException("加载菜单错误，请检查传入参数！Menuid["+menuid+"]");
		}
		return list;
	}
	
	/**
	 * 获取菜单名字
	 * @param menuid
	 * @return
	 */
	public static String getMenuName(String menuid){
		return nameMap.get(menuid);
	}
	
	/**
	 * 加载菜单缓存
	 * @param root
	 */
	@SuppressWarnings("unchecked")
	public static void loadMainMenu(Element root){
		 // 主菜单
		 for (Iterator<Element> ite = root.elementIterator(); ite.hasNext();){
			 List<Map<String,String>> publicMenu = new ArrayList<Map<String,String>>();
			 
			 Element element = ite.next();
			 String menuid = element.attributeValue("id");
			 nameMap.put(menuid, element.attributeValue("name"));
			 // 子菜单
			 for (Iterator<Element> ieson = element.elementIterator(); ieson.hasNext();){
				 Map<String,String> menu = new HashMap<String, String>();
				 Element elementSon = ieson.next();
				 menu.put("url", elementSon.attributeValue("url"));
				 menu.put("name", elementSon.attributeValue("name"));
				 menu.put("iamge", elementSon.attributeValue("iamge"));
				 menu.put("submenu", elementSon.attributeValue("submenu"));
				 
				 publicMenu.add(menu);
			 }
			 Map<String,List<Map<String,String>>> roleMap = new HashMap<String, List<Map<String,String>>>();
			 roleMap.put("public", publicMenu);
			 menuMap.put(menuid, roleMap);
		 }
	}
	
	/**
	 * 重新装载菜单
	 */
	private static void reloadMenu(){
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(menuXmlPath);
			Element root = doc.getRootElement();
			loadMainMenu(root);
			
			File file = new File(menuXmlPath);
			modefiedTime = file.lastModified();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		List<Map<String,String>> list = loadMenu("20000", "public");
		for(Map<String, String> map : list){
			System.out.println(map.get("name"));
			System.out.println(map.get("iamge"));
			System.out.println(map.get("url"));
		}
	}
}